<?php

namespace App\V3\V3_1\Controllers;

use App\V3\V3_1\Models\StorageManageModel;
use App\V3\V3_1\Models\StoragesModel;
use Illuminate\Http\Request;
use App\V3\V3_1\Services\StoragesService;
use Illuminate\Support\Facades\Validator;

class StoragesController extends BaseController
{
    /**
     * 仓库列表
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/7 14:36
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ], [
            'pageSize.required' => '参数`pageSize-页面大小`丢失',
            'currentPage.required' => '参数`currentPage-当前页数`丢失',
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $data['keyword'] = empty($data['keyword'])?'':$data['keyword'];

        return $this->success(
            StoragesService::list(
                $this->getMainUserId(),
                $data
            )
        );
    }

    /**
     * 仓库下拉列表
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/7 14:36
     * @return \Illuminate\Http\JsonResponse
     */
    public function select()
    {
        return $this->success(StoragesService::storageSelect(
            $this->getMainUserId()
        ));
    }

    /**
     * 新建仓库
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/7 14:53
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(),
            [
                'storage_name' =>'required',//仓库名
                'storage_address' =>'required',//仓库地址
                'user_id' =>'required',//仓库负责人id
            ],
            [
                'storage_name.required' => '请填写仓库名称',
                'storage_address.required' => '请填写仓库地址',
                'user_id.required' => '请选择仓库负责人',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if($this->getUserTpye()) {//子账户添加仓库
            $data['user_id'] = $this->getUserId();
        }

        $data['created_at'] = time();
        $data['main_id'] = $this->getMainUserId();
        $data['remarks'] = empty($data['remarks'])?'':$data['remarks'];

        $checkInfo = StoragesService::checkStoragesInfo($data);

        if($checkInfo) {
            return $this->fail('', '仓库`'.$data['storage_name'].'-'.$data['storage_address'].'`已存在');
        }

        $insertId = StoragesModel::insertGetId($data);

        $this->log('添加仓库', $data);
        return $this->success(['id' => $insertId]);
    }

    /**
     * 修改仓库信息
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/8 14:04
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(),
            [
                'id' =>'required|integer',//仓库id标识
                'storage_name' =>'required',//仓库名
                'storage_address' =>'required',//仓库地址
                'user_id' =>'required',//仓库负责人id
            ],
            [
                'id.required' => '参数`库存id标识`丢失',//
                'id.integer' => '参数`库存id标识`类型错误',//
                'storage_name.required' => '请填写仓库名称',
                'storage_address.required' => '请填写仓库地址',
                'user_id.required' => '请选择仓库负责人',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        if($this->getUserTpye()) {//子账户添加仓库
            $data['user_id'] = $this->getUserId();
        }

        $isUpdate = StoragesModel::where('id', $data['id'])
            ->update($data);

        if($isUpdate !== false) {
            $this->log('修改仓库信息', $data);
            return $this->success();
        }
    }

    /**
     * 删除指定仓库
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/8 14:13
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        //验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//供应商客户表主键id
        ],
            [
                'id.required' => '参数`id`丢失',
            ]
        );
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        //检查当前仓库是否有未清零的库存物料信息
        $checkStorageManageInfo = StorageManageModel::where('storage_id', $data['id'])
            ->orderBy('in_storage_num', 'DESC')
            ->first();

        if($checkStorageManageInfo['in_storage_num'] > 0) {
            return $this->fail('', '当前仓库存在商品，请清空库存后删除');
        }

        StoragesModel::where('id', $data['id'])->update(['delete_flag' => 1]);
        $this->log('删除仓库',$data);
        return $this->success();
    }
}
